<?php

namespace App\Models;

use App\Traits\External;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Member_Photo extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_member_photo';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'agencyid','mid','url','status','is_del'
    ];

    public static $status = [
        'wait' => '待审核',
        'pass' => '通过',
        'refuse' => '未通过'
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function scopeNodel($query)
    {
        return $query->where('is_del', 'N');
    }



    protected $hidden = [
        'is_del',
    ];

    public function hasOneMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'mid');
    }


    public static function getFilter($filter)
    {
        return self::whereHas('hasOneMember',function ($query) use ($filter){
            if (isset($filter['username']) && !empty($filter['username'])) {
                $query->where('username', 'LIKE', '%' . $filter['username'] . '%');
            }
            if (isset($filter['mobile']) && !empty($filter['mobile'])) {
                $query->where('mobile', 'LIKE', '%' . $filter['mobile'] . '%');
            }
        })->where(function ($query) use ($filter){
            if (isset($filter['mid']) && !empty($filter['mid'])) {
                $query->where('mid', $filter['mid']);
            }
            if (isset($filter['status']) && !empty($filter['status'])) {
                $query->where('status', $filter['status']);
            }
            $query->where('agencyid', $filter['aid']);

        })->orderBy('id','desc');

    }
}
